[#include "../../../common/html.ftl" /]
[@html]
    [@head title="角色数据权限" css=["fw", "ztree"] /]
    [@body]
        [@formMain ]
            <div class="fw-forms">
                [@dataForm formId="form-role-edit" ]
                    <input id="roleId" name="roleId" type="hidden" value="${sysRole.roleId!}"/>
                    <div class="am-form-group am-u-sm-12 am-u-md-6 am-u-lg-6">
                        <label>角色名称：</label>
                        <input class="am-form-field am-radius" type="text" name="roleName" value="${sysRole.roleName!}" readonly/>
                    </div>
                    <div class="am-form-group am-u-sm-12 am-u-md-6 am-u-lg-6">
                        <label>数据范围：</label>
                        <select class="am-radius"id="dataScope" name="dataScope" title="特殊情况下，设置为“自定数据权限”">
                            [#if sysRole.dataScope?? && sysRole.dataScope == '1']<option value="1" selected>全部数据权限</option>[#else]<option value="1">全部数据权限</option>[/#if]
                            [#if sysRole.dataScope?? && sysRole.dataScope == '2']<option value="2" selected>自定数据权限</option>[#else]<option value="2">自定数据权限</option>[/#if]
                            [#if sysRole.dataScope?? && sysRole.dataScope == '3']<option value="3" selected>本部门数据权限</option>[#else]<option value="3">本部门数据权限</option>[/#if]
                            [#if sysRole.dataScope?? && sysRole.dataScope == '4']<option value="4" selected>本部门及以下数据权限</option>[#else]<option value="4">本部门及以下数据权限</option>[/#if]
                        </select>
                    </div>
                    <div class="am-form-group am-u-sm-12 am-u-md-12 am-u-lg-12" id="authDataScope" style="display: [#if sysRole.dataScope=='2']block[#else ]none[/#if]">
                        <label>数据权限：</label>
                        <div id="deptTrees"class="ztree am-radius" style="border: 1px solid #ccc;"></div>
                    </div>
                [/@dataForm]
            </div>
        [/@formMain]

        [@rightToolbar /]

        [@script js=["fw", "validate", "ztree"] progress="false" ]
            <script type="text/javascript">
                var prefix = ctx + "osfw/system/manage/role";

                $(function() {
                    var url = ctx + "osfw/system/manage/dept/roleDeptTreeData?roleId=" + $("#roleId").val();
                    var options = {
                        id: "deptTrees",
                        url: url,
                        check: { enable: true, nocheckInherit: true, chkboxType: { "Y": "ps", "N": "ps" } },
                        expandLevel: 3
                    };
                    $.tree.init(options);
                });

                function submitHandler() {
                    if ($.validate.form()) {
                        edit();
                    }
                }

                function edit() {
                    var roleId = $("input[name='roleId']").val();
                    var roleName = $("input[name='roleName']").val();
                    var roleKey = $("input[name='roleKey']").val();
                    var dataScope = $("#dataScope").val();
                    var deptIds = $.tree.getCheckedNodes();
                    $.ajax({
                        cache : true,
                        type : "POST",
                        url : ctx + "system/role/authDataScope",
                        data : {
                            "roleId": roleId,
                            "roleName": roleName,
                            "roleKey": roleKey,
                            "dataScope": dataScope,
                            "deptIds": deptIds
                        },
                        async : false,
                        error : function(request) {
                            $.modal.alertError("系统错误");
                        },
                        success : function(data) {
                            $.operate.successCallback(data);
                        }
                    });
                }

                $("#dataScope").change(function(event){
                    var dataScope = $(event.target).val();
                    dataScopeVisible(dataScope);
                });

                function dataScopeVisible(dataScope) {
                    if (dataScope == 2) {
                        $("#authDataScope").show();
                    } else {
                        $._tree.checkAllNodes(false);
                        $("#authDataScope").hide();
                    }
                }
            </script>
        [/@script]
    [/@body]
[/@html]